package com.qs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.BaseDAO;
import com.infomanage.exception.DaoException;
import com.infomanage.po.Department;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.po.Person;
import com.jiuqi.webtree.util.StringUtils;
import com.qs.po.QsCtPhyTest;
import com.qs.vo.IndividualAnalResultItem;
import com.qs.vo.QsCtPhyTestItem;

public class QsCtPhyTestDAO extends BaseDAO<QsCtPhyTest> {
	public QsCtPhyTestItem find(int id) throws DaoException{
		try {
			SearchBean searchBean  = new SearchBean();
			FieldBean fieldBean  = new FieldBean();
			fieldBean.setName("id");
			fieldBean.setValue(id);
			searchBean.add(fieldBean);
			List result = super.findByFields(new Class[] {Person.class , Department.class},
					QsCtPhyTest.class, new String[] { "personId", "deptId" },
					new String[] { "personId", "deptId"}, searchBean);
			List<QsCtPhyTestItem> items = parseResult(result);
			return items.size() == 0 ? null : items.get(0);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	@SuppressWarnings("unchecked")
	public List<QsCtPhyTestItem> findAll() throws DaoException {
		try {
			List result = findAllMutilTable(new Class[] {Person.class , Department.class},
					QsCtPhyTest.class, new String[] { "personId", "deptId" },
					new String[] { "personId", "deptId"});
			return parseResult(result);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<QsCtPhyTestItem> findByFields(SearchBean searchBean) throws DaoException {
		List result = super.findByFields(new Class[] {Person.class , Department.class},
				QsCtPhyTest.class, new String[] { "personId", "deptId" },
				new String[] { "personId", "deptId"}, searchBean);
		return parseResult(result);
	}
	
	public List<IndividualAnalResultItem> findByTraineeAndDate(String[] traineeIds,
			String beginDate, String endDate) throws DaoException {
		if(traineeIds == null || traineeIds.length == 0) {
			return null;
		}
		String hsql = "select T1.person_id,T2.name,T1.TIMESTAMP,T1.SCORE from qs_ct_phy_test"
				+ " as T1 inner join person"
				+ " as T2 on T1.person_id = T2.person_Id " 
				+ " inner join department as T3 on T1.dept_Id = T3.dept_Id " 
				+ " where T1.person_id in (";
		StringBuilder sb = new StringBuilder(hsql);
		for (int i = 0; i < traineeIds.length; i++) {
			if(StringUtils.isNotEmpty(traineeIds[i])) {
				if(i != 0) {
					sb.append(",");
				}
				sb.append(traineeIds[i]);
			}
		}
		sb.append(")");
		sb.append(" and TIMESTAMP >= '" + beginDate + "'");
		sb.append(" and TIMESTAMP <= '" + endDate + "'");
		sb.append(" order by TIMESTAMP");
		List<IndividualAnalResultItem> list = new ArrayList<IndividualAnalResultItem>();
		Session session = HibernateSessionFactory.getSession();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Connection conn = session.connection();
			ps = conn.prepareStatement(sb.toString());
			rs = ps.executeQuery(sb.toString());
			while(rs.next()) {
				IndividualAnalResultItem item = new IndividualAnalResultItem();
				item.setTraineeId(rs.getInt(1));
				item.setTraineeName(rs.getString(2));
				item.setDate(rs.getString(3));
				item.setScore(rs.getFloat(4));
				list.add(item);
			}
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				throw new DaoException(e);
			}
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	public List<QsCtPhyTestItem> parseResult(List result){
		Iterator it = result.iterator();
		List<QsCtPhyTestItem> list = new ArrayList<QsCtPhyTestItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			QsCtPhyTest equ = (QsCtPhyTest) o[0];
			Person person = (Person) o[1];
			Department dept = (Department)o[2];
			QsCtPhyTestItem item = new QsCtPhyTestItem();
			item.setBean(equ);
			item.setPerson(person);
			item.setDept(dept);
			list.add(item);
		}
		return list;
	}
	
	public int count(SearchBean searchBean){
		return super.count(new Class[] {Person.class , Department.class},
				QsCtPhyTest.class, new String[] { "personId", "deptId" },
				new String[] { "personId", "deptId"}, searchBean);
	}
}
